package com.liuhong.shoping.controller;


import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liuhong.shoping.common.ResponseMessage;
import com.liuhong.shoping.dao.MenuNavInfoMapper;
import com.liuhong.shoping.entity.MenuNavInfo;
import com.liuhong.shoping.entity.OrgInfo;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liuhong
 * @since 2021-01-11
 */
@RestController
@RequestMapping("/menuNavInfo")
public class MenuNavInfoController {
	
	@Autowired
	private MenuNavInfoMapper menuNavInfoMapper;
	
	/**
	 * 导航列表
	 * @return
	 */
	@PostMapping("getMenuNavList")
	public ResponseMessage getMenuNavList() {
		try {
			List<MenuNavInfo> navList = menuNavInfoMapper.selectList(null);
			return ResponseMessage.success(navList);
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseMessage.fail();
		}
	}
	
	/**
	 * 
	 * @param { MenuNavInfo }
	 * 删除组织机构 
	 * 以及包含的的子集
	 */
	@Transactional
	@PostMapping("deleteMenuNavInfo")
    public ResponseMessage deleteMenuNavInfo(@RequestBody MenuNavInfo nav) 
	{
		try {
			 int deleteRows = menuNavInfoMapper.deleteById(nav.getId());
			 QueryWrapper<MenuNavInfo> queryWrapperOrgInfo = new QueryWrapper<>();
			 queryWrapperOrgInfo
			 	.lambda()
			 	.eq(MenuNavInfo::getParentsId, nav.getId())
			 	.isNotNull(MenuNavInfo::getParentsId);
			 menuNavInfoMapper.delete(queryWrapperOrgInfo);
			 return ResponseMessage.success(deleteRows,"删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseMessage.fail();
		}
    }
	
	/**
	 * @Param{ MenuNavInfo entity }
	 * 修改导航信息
	 */
	@PostMapping("updateMenuNavInfo")
    public ResponseMessage updateMenuNavInfo(@RequestBody MenuNavInfo nav) 
	{
		try {
			int updateRows = menuNavInfoMapper.updateById(nav);
			return ResponseMessage.success(updateRows,"修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseMessage.fail();
		}
    }
	
	/**
	 * @Param{ MenuNavInfo entity }
	 * 添加导航
	 */
	@PostMapping("addOMenuNavNode")
    public ResponseMessage addOMenuNavNode(@RequestBody MenuNavInfo nav) 
	{
		try {
			nav.setId(UUID.randomUUID().toString());
			nav.setCreatedTime(LocalDateTime.now());
			int insertRows = menuNavInfoMapper.insert(nav);
			return ResponseMessage.success(insertRows,"插入成功");
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseMessage.fail();
		}
    }

}

